<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring Boot 健康检查机制深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .code-block {
            background: #1e1e1e;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-header {
            background: #2d2d2d;
            padding: 8px 16px;
            font-size: 12px;
            color: #888;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        
        .code-dots {
            display: flex;
            gap: 6px;
        }
        
        .dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        
        .dot-red { background: #ff5f56; }
        .dot-yellow { background: #ffbd2e; }
        .dot-green { background: #27c93f; }
        
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
            color: #d4d4d4;
            font-size: 14px;
            line-height: 1.6;
        }
        
        .section-divider {
            height: 2px;
            background: linear-gradient(to right, transparent, #667eea, transparent);
            margin: 3rem 0;
        }
        
        .floating-icon {
            animation: float 3s ease-in-out infinite;
        }
        
        @keyframes float {
            0%, 100% { transform: translateY(0px); }
            50% { transform: translateY(-10px); }
        }
        
        .first-letter {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 8px;
            margin-top: 3px;
            font-weight: 700;
            color: #667eea;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center">
            <div class="floating-icon inline-block mb-6">
                <i class="fas fa-heartbeat text-6xl opacity-90"></i>
            </div>
            <h1 class="text-5xl md:text-6xl font-bold mb-6">Spring Boot 健康检查机制</h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                在微服务架构的世界里，健康检查如同系统的脉搏，时刻守护着应用的生命力
            </p>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 card-hover">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="first-letter">在</span>现代微服务架构中，健康检查机制对于确保系统的稳定性和可靠性至关重要。Spring Boot 提供了内置的健康检查支持，主要通过 Spring Boot Actuator 模块实现。
            </p>
        </div>

        <!-- Concept Section -->
        <section class="mb-12">
            <div class="flex items-center mb-6">
                <i class="fas fa-lightbulb text-3xl text-purple-600 mr-4"></i>
                <h2 class="text-3xl font-bold text-gray-800">健康检查的概念</h2>
            </div>
            
            <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                <p class="text-gray-700 leading-relaxed mb-6">
                    健康检查是指定期检测应用程序的运行状态，确保其处于正常工作状态。这包括监测应用的各种资源（如数据库连接、消息队列、缓存等）的可用性，以及应用本身的性能指标。
                </p>
                
                <div class="grid md:grid-cols-2 gap-6">
                    <div class="bg-green-50 rounded-xl p-6 border-2 border-green-200">
                        <div class="flex items-center mb-3">
                            <i class="fas fa-check-circle text-green-600 text-2xl mr-3"></i>
                            <h3 class="text-xl font-bold text-green-800">UP 状态</h3>
                        </div>
                        <p class="text-gray-700">表示服务正常运行，所有组件工作正常</p>
                    </div>
                    
                    <div class="bg-red-50 rounded-xl p-6 border-2 border-red-200">
                        <div class="flex items-center mb-3">
                            <i class="fas fa-times-circle text-red-600 text-2xl mr-3"></i>
                            <h3 class="text-xl font-bold text-red-800">DOWN 状态</h3>
                        </div>
                        <p class="text-gray-700">表示服务不可用，存在故障或异常</p>
                    </div>
                </div>
            </div>
        </section>

        <div class="section-divider"></div>

        <!-- Spring Boot Actuator Section -->
        <section class="mb-12">
            <div class="flex items-center mb-6">
                <i class="fas fa-cogs text-3xl text-purple-600 mr-4"></i>
                <h2 class="text-3xl font-bold text-gray-800">Spring Boot Actuator</h2>
            </div>
            
            <div class="bg-white rounded-2xl shadow-lg p-8 mb-8 card-hover">
                <p class="text-gray-700 leading-relaxed">
                    <strong class="text-purple-600">Spring Boot Actuator</strong> 是一个用于监控和管理 Spring Boot 应用的工具，提供了一系列的生产就绪特性。它提供了多个端点，可以获取应用程序的运行状况、性能指标、应用信息等。其中，<code class="bg-gray-100 px-2 py-1 rounded text-purple-600">/actuator/health</code> 是健康检查的主要端点。
                </p>
            </div>

            <!-- Configuration -->
            <div class="mb-8">
                <h3 class="text-2xl font-bold text-gray-800 mb-4 flex items-center">
                    <i class="fas fa-wrench text-purple-600 mr-3"></i>
                    端点配置
                </h3>
                
                <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                    <p class="text-gray-700 mb-4">在 Spring Boot 中，可以通过以下步骤启用和配置健康检查端点：</p>
                    
                    <div class="space-y-4">
                        <div class="flex items-start">
                            <i class="fas fa-chevron-right text-purple-600 mt-1 mr-3"></i>
                            <p class="text-gray-700">在 <code class="bg-gray-100 px-2 py-1 rounded">pom.xml</code> 或 <code class="bg-gray-100 px-2 py-1 rounded">build.gradle</code> 中添加 <code class="bg-gray-100 px-2 py-1 rounded">spring-boot-starter-actuator</code> 依赖。</p>
                        </div>
                        
                        <div class="flex items-start">
                            <i class="fas fa-chevron-right text-purple-600 mt-1 mr-3"></i>
                            <p class="text-gray-700">在 <code class="bg-gray-100 px-2 py-1 rounded">application.properties</code> 或 <code class="bg-gray-100 px-2 py-1 rounded">application.yml</code> 中配置 Actuator 的端点暴露：</p>
                        </div>
                    </div>
                    
                    <div class="code-block mt-6">
                        <div class="code-header">
                            <div class="code-dots">
                                <span class="dot dot-red"></span>
                                <span class="dot dot-yellow"></span>
                                <span class="dot dot-green"></span>
                            </div>
                            <span>application.properties</span>
                        </div>
                        <pre><code>management.endpoints.web.exposure.include=health,info</code></pre>
                    </div>
                </div>
            </div>

            <!-- Health Endpoint -->
            <div>
                <h3 class="text-2xl font-bold text-gray-800 mb-4 flex items-center">
                    <i class="fas fa-heartbeat text-purple-600 mr-3"></i>
                    健康检查端点
                </h3>
                
                <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                    <p class="text-gray-700 mb-6">访问 <code class="bg-gray-100 px-2 py-1 rounded text-purple-600">/actuator/health</code> 端点时，Spring Boot 将返回应用程序的健康状态信息。返回的数据结构通常如下：</p>
                    
                    <div class="code-block">
                        <div class="code-header">
                            <div class="code-dots">
                                <span class="dot dot-red"></span>
                                <span class="dot dot-yellow"></span>
                                <span class="dot dot-green"></span>
                            </div>
                            <span>JSON Response</span>
                        </div>
                        <pre><code>{
  "status": "UP",
  "components": {
    "diskSpace": {
      "status": "UP",
      "details": {
        "total": 50000000000,
        "free": 40000000000,
        "threshold": 10000000
      }
    },
    "database": {
      "status": "UP"
    }
  }
}</code></pre>
                    </div>
                </div>
            </div>
        </section>

        <div class="section-divider"></div>

        <!-- Custom Health Check Section -->
        <section class="mb-12">
            <div class="flex items-center mb-6">
                <i class="fas fa-code text-3xl text-purple-600 mr-4"></i>
                <h2 class="text-3xl font-bold text-gray-800">自定义健康检查</h2>
            </div>
            
            <div class="bg-white rounded-2xl shadow-lg p-8 mb-8 card-hover">
                <p class="text-gray-700 leading-relaxed">
                    Spring Boot Actuator 允许开发人员自定义健康检查，便于监控特定的应用组件。可以通过实现 <code class="bg-gray-100 px-2 py-1 rounded text-purple-600">HealthIndicator</code> 接口来自定义健康检查逻辑。
                </p>
            </div>

            <div>
                <h3 class="text-2xl font-bold text-gray-800 mb-4 flex items-center">
                    <i class="fas fa-file-code text-purple-600 mr-3"></i>
                    自定义实现
                </h3>
                
                <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                    <p class="text-gray-700 mb-6">例如，如果需要检查某个外部服务的可用性，可以创建一个自定义的 <code class="bg-gray-100 px-2 py-1 rounded">HealthIndicator</code>：</p>
                    
                    <div class="code-block">
                        <div class="code-header">
                            <div class="code-dots">
                                <span class="dot dot-red"></span>
                                <span class="dot dot-yellow"></span>
                                <span class="dot dot-green"></span>
                            </div>
                            <span>MyServiceHealthIndicator.java</span>
                        </div>
                        <pre><code>import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class MyServiceHealthIndicator implements HealthIndicator {

    @Override
    public Health health() {
        // 进行健康检查
        boolean serviceUp = checkMyService();
        
        if (serviceUp) {
            return Health.up().build();
        } else {
            return Health.down().withDetail("Error Code", "Service is down").build();
        }
    }

    private boolean